﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FTPLib;
using System.IO;
using System.Threading;

namespace WebCam
{
    public partial class UploadIm : Form
    {
        private string path;
        private string limba;
        private string ip;
        private string user;
        private string pass;
        private int port;

        public bool success;

        public UploadIm(string limba, string path, string ip, string user, string pass, int port)
        {
            InitializeComponent();
            Rename(limba);

            this.path = path;
            this.limba = limba;
            this.ip = ip;
            this.user = user;
            this.pass = pass;
            this.port = port;
        }

        private void UploadIm_Shown(object sender, EventArgs e)
        {
            this.Refresh();
            FTP ftpClient = new FTP();
            success = true;
            int percent;
            int fileNr;
            int fileCur = 0;
            long totalSize = 0;
            long transferatedSize = 0;
            string file = "";

            try
            {
                ftpClient.port = port;
                ftpClient.server = ip;
                ftpClient.user = user;
                ftpClient.pass = pass;

                // Get all files for transfer
                DirectoryInfo di = new DirectoryInfo(Application.StartupPath + "\\" + path);
                FileInfo[] fileList = di.GetFiles("*.jpg");
                fileNr = fileList.Length;

                // Calculate total size
                foreach (FileInfo fi in fileList)
                {
                    totalSize += fi.Length;
                }

                // Do actual upload
                ftpClient.MakeDir(path);
                ftpClient.ChangeDir(path);
                foreach (FileInfo fi in fileList)
                {
                    fileCur++;
                    lbCount.Text = fileCur.ToString() + "/" + fileNr;
                    this.Refresh();

                    file = fi.Name;
                    ftpClient.OpenUpload(path + file, file);

                    while (ftpClient.DoUpload() > 0)
                    {
                        percent = (int)(((ftpClient.BytesTotal + transferatedSize) * 100) / totalSize);
                        progressBar.Value = percent;
                    }

                    transferatedSize += fi.Length;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error uploading file " + file + " .\r\n" +
                                "Reason: " + ex.Message,
                                "Error",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);
                success = false;
            }

            success = true;
            this.Hide();
        }
    }
}
